package queue;

import array.Array;

/**
 * @author lynn
 * @date 18-12-14 上午5:52
 */
public class ArrayQueue<E> implements Queue<E>  {

    private Array<E> array;

    public ArrayQueue(int capacity) {
        this.array = new Array<>(capacity);
    }

    public ArrayQueue() {
        this.array = new Array<>();
    }

    /**
     * get queue size
     *
     * @return
     */
    @Override
    public int getSize() {
        return this.array.getSize();
    }

    /**
     * get queue is empty or not
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return this.array.isEmpty();
    }

    /**
     * put an element to the queue
     *
     * @param e
     */
    @Override
    public void enqueue(E e) {
        this.array.addLast(e);
    }

    /**
     * get capacity
     * @return
     */
    public int getCapacity(){
        return this.array.getCapacity();
    }

    /**
     * delete an element from the queue
     *
     * @return
     */
    @Override
    public E dequeue() {
        return this.array.removeFrist();
    }

    /**
     * get an element from the queue
     *
     * @return
     */
    @Override
    public E getFront() {
        return this.array.getFirst();
    }

    /**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append("Queue: ");
        res.append("front [");
        for (int i = 0; i < this.array.getSize(); i++) {
            res.append(this.array.get(i));
            if (i != this.array.getSize() - 1) {
                res.append(",");
            }
        }
        res.append("] tail");
        return res.toString();
    }
}
